package com.polaris.server.bo;

import com.polaris.server.pojo.SysMenu;
import com.polaris.server.pojo.SysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author CNPolaris
 * @version 1.0
 */
public class AdminUserDetails implements UserDetails {
    private final SysUser sysUser;
    private final List<SysMenu> sysMenuList;

    public AdminUserDetails(SysUser sysUser, List<SysMenu> sysMenuList) {
        this.sysUser = sysUser;
        this.sysMenuList = sysMenuList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return sysMenuList.stream()
                .map(menu -> new SimpleGrantedAuthority(menu.getId()+":"+menu.getMenuName()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return sysUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return sysUser.getEnable().equals(1);
    }
}
